package laba3;

import interfaces.task3.StringUtils;
import java.util.Arrays;

/**
 *
 * @author yakimovich
 */
public class StringUtilsImpl implements StringUtils {

    /**
     * Inverts the specified string
     *
     * @param string specified string
     * @return inverted string
     */
    @Override
    public String invert(String string) {
        if (string == null) {
            return "";
        }
        StringBuilder sb = new StringBuilder(string);
        sb.reverse();
        return sb.toString();
    }

    /**
     * finds all characters that appear in the first word, but not in the second
     *
     * @param string
     * @param string1
     * @return a string of characters that are present in the first word, but
     * not in the second
     */
    @Override
    public String compareWords(String string, String string1){
        StringBuilder sb = new StringBuilder("");
        for (int i = 0; i < string.length(); i++) {
            char ch = string.charAt(i);
            if (string1.indexOf(ch) == -1) {
                sb.append(ch);
            }
        }
        return sb.toString();
    }

    /**
     * Finds the first number of type double in the specified string if it's
     * there
     *
     * @param string
     * @return double value of specified string
     */
    @Override
    public double parseDouble(String string){
        double result;
        String[] arr = string.split(" ");
        try {
        result = Double.parseDouble(arr[0]);
        }catch (NumberFormatException e){
            throw new IllegalArgumentException("In the specified line number is not found", e);
        }
        return result;
    }
}